<div id="editmap_container"><div id="map"></div></div>

<% content_for :page_scripts do %>
<script src="//maps.google.com/maps/api/js?v=3.8&amp;sensor=false&amp;key=&amp;libraries=geometry&amp;language=&amp;hl=&amp;region=" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
  // ================= globals ==================

  var marker = null;

  // ================= functions =================

  function initialize() {
    var mapOptions = {
      zoom: 1,
      center: new google.maps.LatLng(37.269174,-119.306607),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map'),mapOptions);
    geocoder = new google.maps.Geocoder();

    google.maps.event.addListener(map, 'rightclick', function(event) {
      remove_add_marker();
      place_add_marker(event.latLng);
    });

    google.maps.event.addListener(map, 'click', function(event) {
      if(openMarker != null && openInfoWindow != null){
        openInfoWindow.close();
        openMarker = null;
        openInfoWindow = null;
      }
    });

    pb = progressBar();
    map.controls[google.maps.ControlPosition.RIGHT].push(pb.getDiv());

    google.maps.event.addListener(map, 'bounds_changed', function() {
      google.maps.event.addListenerOnce(map, 'idle', function(){
        var zoom = map.getZoom();
        var bounds = map.getBounds();
        if(prior_bounds == null && prior_zoom == null){
          prior_zoom = zoom;
          prior_bounds = bounds;
          return;
        }
        if(zoom > 11 && prior_bounds != bounds){
          do_markers(bounds,null,true);
        }
        prior_zoom = zoom;
        prior_bounds = bounds;
      });
    });
  
    var lat = $("location_lat").value;
    var lng = $("location_lng").value;
    marker = new google.maps.Marker({
      icon: '',
      position: new google.maps.LatLng(lat,lng), 
      map: map,
      draggable: true,
    });
    map.setZoom(15);
    map.panTo(marker.getPosition());
    var nag = new google.maps.InfoWindow({
      content: '<div id="editmarker"><b>Please adjust the marker to<br>the position of the source.</b><br><br><i>Check the satellite view - your<br>plant may be visible from space!</i></div>'
    });
    nag.open(map,marker);  
    google.maps.event.addListener(marker, 'dragend', function() {
      $("location_lat").value = this.getPosition().lat();
      $("location_lng").value = this.getPosition().lng();
    });
    // update markers once we're done panning and zooming
    google.maps.event.addListenerOnce(map, 'idle', function(){
      do_markers(map.getBounds(),$("location_id").value,true);
    });
  }

  // ================== main-level code ===================

  google.maps.event.addDomListener(window, 'load', initialize);

</script>
<%= javascript_include_tag "mapcommon" %>
<%= javascript_include_tag "maplabel" %>
<%= javascript_include_tag "progressBar" %>
<%= javascript_include_tag "richmarker" %>
<% end %>

<%= render 'form' %>
